package cn.edu.nuc.film.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.Function;
import cn.edu.nuc.film.entity.Review;
import cn.edu.nuc.film.entity.Role;
import cn.edu.nuc.film.entity.User;
import cn.edu.nuc.film.service.interfaces.FilmService;
import cn.edu.nuc.film.service.interfaces.ReviewService;
import cn.edu.nuc.film.service.interfaces.RoleService;
import cn.edu.nuc.film.service.interfaces.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private FilmService filmService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ReviewService reviewService;
	/**
	 * 进入film信息界面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/pageFilmDetail",method=RequestMethod.GET)
	public String pageFilmDetail( int filmId,Model model){
		
		//获取film的详细信息
		Film film = filmService.pageFilmDetail(filmId);
		//获取film的评论
		List<Review> reviewList = reviewService.reviewList(filmId);
		model.addAttribute("film", film);
		model.addAttribute("reviewList", reviewList);
		return "/page/film_info";
	}
	
	
	
	@RequestMapping(value="/index", method = RequestMethod.GET)
	public String login1(Model model,HttpServletRequest request) {
		List<Film> indexFilmList = filmService.indexFilmList();
		model.addAttribute("indexFilmList", indexFilmList);
		return "/page/index";
	}
	
	// 前台登陆界面 ---- 登录操作
	@RequestMapping(value = "/indexlogin", method = RequestMethod.GET)
	public String login1() {
		return "/page/indexlogin";
	}
	// 前台登陆界面 ---- 注册操作
	@RequestMapping(value = "/indexregist", method = RequestMethod.GET)
	public String goindexregist() {
		return "/page/indexregist";
	}
	
	
	// 前台登陆界面 ---- 注册操作
	@RequestMapping(value = "/indexregist", method = RequestMethod.POST)
	public String indexregist(User user) {
		
		int count = userService.insert(user);
		if( count> 0){
			return "redirect:indexlogin";
		}else{
			return "page/indexregist";
		}
		
	}
	
	// 后台登陆界面 ---- 登录注册操作
	@RequestMapping(value = "/managerlogin", method = RequestMethod.GET)
	public String login() {
		return "managerlogin";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(User user, HttpSession session, Model model) {

		User user1 = userService.login(user);

		// 通过user对象进行登录判断（是否跟数据库里的username和userpwd一样）
		if ( !user.getPassword().equals(user1.getPassword())) {
			model.addAttribute("msg", "用户名密码错误");
			// 若username或userpwd 不一致 则获取的user为空 （即登录失败，重新进行登录）
			return "managerlogin";
		} else{
			// 否则登录成功，查到后并将user放到session里，进行传递
			session.setAttribute("user", user1);
			// 将user信息传到显示界面
			model.addAttribute("user", user1);
			if( user1.getRoleid() != 5){
				// 初始化后台界面（后台界面---管理员）
				List<Function> list = userService.initpage(user1);
				model.addAttribute("list", list);
				return "managerindex";
			}else{
				//用户页面
				List<Film> indexFilmList = filmService.indexFilmList();
				model.addAttribute("indexFilmList", indexFilmList);
				return "/page/index";
			}
			
		}
	}
	
	
	/**
	 * 未写完  ？？？？
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goRegister", method = RequestMethod.GET)
	public String goRegister(Model model) {
		// 获取角色名称
		List<Role> rolelist = roleService.roleList();
		model.addAttribute("rolelist", rolelist);
		return "managerregister";
	}

	@RequestMapping(value = "/managerregister", method = RequestMethod.POST)
	public String register(User user, HttpSession session) {
		
		
		int count = userService.insert(user);
		
		return "redirect:managerlogin";

		
	}
	
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/indexlogout", method = RequestMethod.GET)
	public String indexlogout(HttpSession session) {

		session.removeAttribute("user");
		return "redirect:indexlogin";

	}
	
	
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {

		session.removeAttribute("user");
		return "redirect:managerlogin";

	}
	
	
	/**
	 * 获取用户信息(分页)
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/film/user/userlist", method = RequestMethod.GET)
	public String userlist(String sname, HttpServletRequest request) {

		// 获取要查询的名字
		// String sname = request.getParameter("sname");
		if (sname == null || sname.equals("")) {
			sname = null;
		}
		// 设置分页信息
		PageControl pc = new PageControl();

		// 设置初始当前页currindex = 1
		Integer currindex = 1;

		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if (request.getParameter("index") != null) {
			currindex = Integer.parseInt(request.getParameter("index"));
		}

		// 将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);

		// 调用service中的分页方法，获取分页信息
		Pager<User> pager = userService.userlist(pc, sname);
		// 将分页信息传送到前台
		request.setAttribute("pager", pager);
		return "sys/user/list";
	}

	/**
	 * 进入添加界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/film/user/toaddUser", method = RequestMethod.GET)
	public String toaddUser(Model model) {
		// 获取角色名称
		List<Role> rolelist = roleService.roleList();
		model.addAttribute("rolelist", rolelist);
		return "sys/user/add";
	}

	/**
	 * 执行添加操作
	 * 
	 * @return
	 /*
	@RequestMapping(value = "/film/user/addUser", method = RequestMethod.POST)
	public String addUser(User user) {
		int count = userService.insert(user);
		return "redirect:userlist";
	}

	/**
	 * 获取user的详细信息
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/film/user/detail", method = RequestMethod.GET)
	public String detail(int userid, Model model) {
		User user = userService.selectByPrimaryKey(userid);
		model.addAttribute("user", user);
		return "null";
	}

	@RequestMapping(value = "/film/user/initRole", method = RequestMethod.GET)
	public String initRole(int userid, Model model) {

		// 获取角色名称
		List<Role> rolelist = roleService.roleList();
		model.addAttribute("rolelist", rolelist);

		User user = userService.selectByPrimaryKey(userid);
		model.addAttribute("user", user);
		return "sys/user/role";
	}

	/**
	 * 进入编辑界面
	 * 
	 * @param userid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/film/user/toeditUser", method = RequestMethod.GET)
	public String toeditUser(int userid, Model model) {

		// 获取角色名称
		List<Role> rolelist = roleService.roleList();
		model.addAttribute("rolelist", rolelist);

		User user = userService.selectByPrimaryKey(userid);
		model.addAttribute("item", user);
		return "sys/user/edit";
	}

	/**
	 * 执行编辑操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "/film/user/editUser", method = RequestMethod.POST)
	public String editUser(User user) {

//		int count = 
		userService.updateByPrimaryKeySelective(user);
		return "redirect:userlist";
	}

	/**
	 * 执行删除用户操作
	 * 
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/film/user/deleteUser", method = RequestMethod.GET)
	public String deleteUser(int userid) {
		/*int count = */
		userService.deleteByPrimaryKey(userid);
		return "redirect:userlist";

	}

}
